package msdark.cliente;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;



public class Mensaje {
        public enum MsgType{
                MSG,LIST,VIDEO,IMG,ERROR
        }
        private MsgType tipo;
        private String contenido;
        private String clientID;
        
        private String archivo;
        
        public String getContenido(){
                return contenido;
        }
        
        public MsgType getTipo(){
                return tipo;
        }
        
        public String getID(){
                return clientID;
        }
        
        public void setContenido(String content){
                contenido = content;
        }
        
        public void setTipo(MsgType type){
                tipo = type;
        }
        
        public void setID(String id){
                clientID = id;
        }
        
        public void show(){
                System.out.println("Contenido : "+contenido);
                System.out.println("Tipo : "+tipo.toString());
                System.out.println("clientID : "+clientID);
        }
        
        public String Serialize(String directorio) throws FileNotFoundException, IOException, ParserConfigurationException, TransformerException{
                
                DocumentBuilderFactory documentBuilderFactory = 
             DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = 
                         documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.newDocument();
                document.setXmlStandalone(true);
                document.setXmlVersion("1.0");
                Element rootElement = document.createElement("Mensaje");
                document.appendChild(rootElement);
                
                Element em = document.createElement("Tipo");
                em.appendChild(document.createTextNode(getTipo().toString()));
                rootElement.appendChild(em);
                
                em = document.createElement("contenido");
                em.appendChild(document.createTextNode(getContenido().toString()));
                rootElement.appendChild(em);
                
                em = document.createElement("clientID");
                em.appendChild(document.createTextNode(getID().toString()));
                rootElement.appendChild(em);
                
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result =  new StreamResult(directorio+"serialize"+clientID+".xml");
            transformer.transform(source, result);
                archivo = directorio+"serialize"+clientID+".xml"; 
                return archivo;
        }
        
        public void Deserialize(File file){
                try{
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse(file);
                        doc.getDocumentElement().normalize();
                        NodeList nodeLst = doc.getElementsByTagName("Mensaje");
                        for (int s = 0; s < nodeLst.getLength(); s++) {
                                Node fstNode = nodeLst.item(s);
                                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                                        Element fstElmnt = (Element) fstNode;
                                        NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Tipo");
                                        Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                                        NodeList lstNm = lstNmElmnt.getChildNodes();
                                        
                                        String tipo = ((Node) lstNm.item(0)).getNodeValue().toString();
                                        for(MsgType t : MsgType.values()){
                                                if(t.toString().compareTo(tipo)==0){
                                                        this.tipo = MsgType.MSG;
                                                        break;
                                                }else{
                                                        if(t.toString().compareTo(tipo)==0){
                                                                this.tipo = MsgType.LIST;
                                                                break;
                                                        }else{
                                                                if(t.toString().compareTo(tipo)==0){
                                                                        this.tipo = MsgType.VIDEO;
                                                                        break;
                                                                }else{
                                                                        if(t.toString().compareTo(tipo)==0){
                                                                                this.tipo = MsgType.ERROR;
                                                                                break;
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                        lstNmElmntLst = fstElmnt.getElementsByTagName("contenido");
                                        lstNmElmnt = (Element) lstNmElmntLst.item(0);
                                        lstNm = lstNmElmnt.getChildNodes();
                                        this.contenido = ((Node) lstNm.item(0)).getNodeValue().toString();
                                        
                                        
                                        lstNmElmntLst = fstElmnt.getElementsByTagName("clientID");
                                        lstNmElmnt = (Element) lstNmElmntLst.item(0);
                                        lstNm = lstNmElmnt.getChildNodes();
                                        this.clientID = ((Node) lstNm.item(0)).getNodeValue().toString();
                                        
                                }
                  }
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
        
        
}
